﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace SDB2DM.SpatialType
{
    class Point : Spatial
    {
        #region 定数
        private const string ID_PREFIX = Utility.SPATIAL_ID_PREFIX_POINT;
        private const string POSITION_TAG = Utility.SPATIAL_POINT_POSITION_TAG;
        private const string DIRECT_POSITION_TAG = Utility.SPATIAL_DIRECT_POSITION_TAG;
        private const string COORDINATE_TAG = Utility.SPATIAL_COORDINATE_TAG;
        private const string DIMENSION_TAG = Utility.SPATIAL_DIMENSION_TAG;
        private const string DIMENSION_VALUE = Utility.SPATIAL_DIMENSION_VALUE;
        #endregion

        /*変数*/
        //private XmlDocument XMLDoc = null;
        //private XmlNode objectNode = null;
        //private string strFeature = string.Empty;

        public Point(XmlDocument XMLDoc, XmlNode objectNode, string strFeature)
            : base(XMLDoc, objectNode, strFeature, Spatial.POINT_TAG)
        {
            //this.XMLDoc = XMLDoc;
            //this.objectNode = objectNode;
            //this.strFeature = strFeature;
        }

        private double x = 0D;
        public double X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        private double y = 0D;
        public double Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        /*
        private string positionTag = String.Empty;
        public string PositionTag
        {
            get
            {
                return Utility.SPATIAL_POINT_POSITION_TAG;
            }
        }

        private string directPosTag = String.Empty;
        public string DirectPosTag
        {
            get
            {
                return Utility.SPATIAL_DIRECT_POSITION_TAG;
            }
        }

        private string coordinateTag = String.Empty;
        public string CoordinateTag
        {
            get
            {
                return Utility.SPATIAL_COORDINATE_TAG;
            }
        }
        */

        private string coordinateValue = String.Empty;
        public string CoordinateValue
        {
            get
            {
                return coordinateValue;
            }
            set
            {
                coordinateValue = value;
            }
        }

        /*
        private string dimensionTag = String.Empty;
        public string DimensionTag
        {
            get
            {
                return Utility.SPATIAL_DIMENSION_TAG;
            }
        }

        private string dimensionValue = String.Empty;
        public string DimensionValue
        {
            get
            {
                return Utility.SPATIAL_DIMENSION_VALUE;
            }
        }
        */

        public override void BuildXMLForSpatialSchema(soGeometry objMetry)
        {
            XmlNode pointNode = null;
            XmlNode pointPosNode = null;
            XmlNode directPosNode = null;
            XmlNode coordinateNode = null;
            XmlNode dimensionNode = null;
            //double pointX = 0D;
            //double pointY = 0D;
            soGeoPoint objPoint = null;
            //const int PRECISION = 3;

            objPoint = (soGeoPoint)objMetry;

            //Node
            pointNode = XMLDoc.CreateElement(
                Utility.CM_JPS_TAG, POINT_TAG, Utility.JPS_URI);
            pointPosNode = XMLDoc.CreateElement(
                Utility.CM_JPS_TAG, POSITION_TAG, Utility.JPS_URI);
            directPosNode = XMLDoc.CreateElement(
                Utility.CM_JPS_TAG, DIRECT_POSITION_TAG, Utility.JPS_URI);
            coordinateNode = XMLDoc.CreateElement(COORDINATE_TAG);
            dimensionNode = XMLDoc.CreateElement(DIMENSION_TAG);

            //Attribute & Value
            (pointNode as XmlElement).SetAttribute(
                Utility.CM_ATTRIBUTE_ID_TAG,
                ID_PREFIX + String.Format("{0, 0:D4}",
                ++Utility.idxPoint));    //0:0000
            coordinateNode.InnerText = String.Format("{0, 0:F3} {1, 0:F3}", objPoint.x, objPoint.y);
            dimensionNode.InnerText = DIMENSION_VALUE;

            //Append
            ObjectNode.AppendChild(pointNode);
            pointNode.AppendChild(pointPosNode);
            pointPosNode.AppendChild(directPosNode);
            directPosNode.AppendChild(coordinateNode);
            directPosNode.AppendChild(dimensionNode);

            Utility.datasetPoint[ID_PREFIX + String.Format("{0, 0:D4}", Utility.idxPoint)] = STRFeature;
            Utility.pointPair[objPoint.CentroidPoint.x] = ID_PREFIX + String.Format("{0, 0:D4}", Utility.idxPoint);
            //if (STRFeature.Contains(Curve.ID_PREFIX))
            //{
            //    Utility.pointsForCurve[objPoint.CentroidPoint] = STRFeature;
            //}

            //Marshal.ReleaseComObject(objPoint);
            //objPoint = null;
        }
    }
}
